package leetcode_401_500;
public class LeeCode_424 {
    public static void main(String[] args) {
        System.out.println(characterReplacement("ABAB", 2));
    }
    private static int characterReplacement(String s, int k) {
        int n = s.length();
        int[] freq = new int[26];
        char[] cs = s.toCharArray();
        int l = 0, r = 0, max = 0;
        while (r < n){
            freq[cs[r] - 'A']++;
            max = Math.max(max, freq[cs[r] - 'A']);
            if (r - l + 1 > max + k){
                freq[cs[l] - 'A']--;
                l++;
            }
            r++;
        }
        return r - l;
    }
}
